/*  1:   */ package nl.siegmann.epublib.util;
/*  2:   */ 
/*  3:   */ import java.io.File;
/*  4:   */ import java.io.FileInputStream;
/*  5:   */ import java.io.FileNotFoundException;
/*  6:   */ import java.io.IOException;
/*  7:   */ import java.io.InputStream;
/*  8:   */ import nl.siegmann.epublib.domain.MediaType;
/*  9:   */ import nl.siegmann.epublib.domain.Resource;
/* 10:   */ import nl.siegmann.epublib.service.MediatypeService;
/* 11:   */ import org.apache.commons.io.IOUtils;
/* 12:   */ import org.apache.commons.vfs.FileContent;
/* 13:   */ import org.apache.commons.vfs.FileName;
/* 14:   */ import org.apache.commons.vfs.FileObject;
/* 15:   */ import org.apache.commons.vfs.FileSystemException;
/* 16:   */ import org.apache.commons.vfs.FileSystemManager;
/* 17:   */ import org.apache.commons.vfs.VFS;
/* 18:   */ import org.slf4j.Logger;
/* 19:   */ import org.slf4j.LoggerFactory;
/* 20:   */ 
/* 21:   */ public class VFSUtil
/* 22:   */ {
/* 23:27 */   private static final Logger log = LoggerFactory.getLogger(VFSUtil.class);
/* 24:   */   
/* 25:   */   public static Resource createResource(FileObject rootDir, FileObject file, String inputEncoding)
/* 26:   */     throws IOException
/* 27:   */   {
/* 28:30 */     MediaType mediaType = MediatypeService.determineMediaType(file.getName().getBaseName());
/* 29:31 */     if (mediaType == null) {
/* 30:32 */       return null;
/* 31:   */     }
/* 32:34 */     String href = calculateHref(rootDir, file);
/* 33:35 */     Resource result = new Resource(null, IOUtils.toByteArray(file.getContent().getInputStream()), href, mediaType);
/* 34:36 */     result.setInputEncoding(inputEncoding);
/* 35:37 */     return result;
/* 36:   */   }
/* 37:   */   
/* 38:   */   public static String calculateHref(FileObject rootDir, FileObject currentFile)
/* 39:   */     throws IOException
/* 40:   */   {
/* 41:41 */     String result = currentFile.getName().toString().substring(rootDir.getName().toString().length() + 1);
/* 42:42 */     result = result + ".html";
/* 43:43 */     return result;
/* 44:   */   }
/* 45:   */   
/* 46:   */   public static FileObject resolveFileObject(String inputLocation)
/* 47:   */     throws FileSystemException
/* 48:   */   {
/* 49:53 */     FileObject result = null;
/* 50:   */     try
/* 51:   */     {
/* 52:55 */       result = VFS.getManager().resolveFile(inputLocation);
/* 53:   */     }
/* 54:   */     catch (Exception e)
/* 55:   */     {
/* 56:   */       try
/* 57:   */       {
/* 58:58 */         result = VFS.getManager().resolveFile(new File("."), inputLocation);
/* 59:   */       }
/* 60:   */       catch (Exception e1)
/* 61:   */       {
/* 62:60 */         log.error(e.getMessage(), e);
/* 63:61 */         log.error(e1.getMessage(), e);
/* 64:   */       }
/* 65:   */     }
/* 66:64 */     return result;
/* 67:   */   }
/* 68:   */   
/* 69:   */   public static InputStream resolveInputStream(String inputLocation)
/* 70:   */     throws FileSystemException
/* 71:   */   {
/* 72:76 */     InputStream result = null;
/* 73:   */     try
/* 74:   */     {
/* 75:78 */       result = VFS.getManager().resolveFile(inputLocation).getContent().getInputStream();
/* 76:   */     }
/* 77:   */     catch (Exception e)
/* 78:   */     {
/* 79:   */       try
/* 80:   */       {
/* 81:81 */         result = new FileInputStream(inputLocation);
/* 82:   */       }
/* 83:   */       catch (FileNotFoundException e1)
/* 84:   */       {
/* 85:83 */         log.error(e.getMessage(), e);
/* 86:84 */         log.error(e1.getMessage(), e);
/* 87:   */       }
/* 88:   */     }
/* 89:87 */     return result;
/* 90:   */   }
/* 91:   */ }


/* Location:           C:\Users\David\workspace\CosaRaraPorqueSi\main\epublib-tools-3.1.jar
 * Qualified Name:     nl.siegmann.epublib.util.VFSUtil
 * JD-Core Version:    0.7.0.1
 */